package com.zhuojy.equity.mall.admin.web.controller;

import java.security.Principal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuojy.equity.mall.admin.common.constant.JwtConstants;
import com.zhuojy.equity.mall.admin.common.entity.TUser;
import com.zhuojy.equity.mall.admin.common.exception.ExceptionUtil;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.common.support.Result;
import com.zhuojy.equity.mall.admin.common.utils.LogUtil;
import com.zhuojy.equity.mall.admin.manager.token.TokenManager;
import com.zhuojy.equity.mall.admin.service.UserService;
import com.zhuojy.equity.mall.admin.service.dto.req.AllotRoleReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.DeleteUserReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.LoginReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.ModifyPasswordReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.ModifyUserInfoReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.QueryUserInfoByIdReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.QueryUserRolesReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.req.RegisterReqDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.AllotRoleResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.DeleteUserResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.LoginResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.ModifyPasswordResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.ModifyUserInfoResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.QueryUserInfoByIdResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.QueryUserInfoResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.QueryUserRolesResDTO;
import com.zhuojy.equity.mall.admin.service.dto.res.RegisterResDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 后台用户管理Controller
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户模块",value = "用户模块")
public class UserController {

	static Logger log = LoggerFactory.getLogger(UserController.class);

	private final UserService userService;

	private final TokenManager tokenManager;


	/**
	 * 用户注册
	 */
	@PostMapping(value = "/register")
	@ApiOperation(value = "注册",httpMethod = "POST",notes = "注册")
	public Result<RegisterResDTO> register(@RequestBody RegisterReqDTO registerReqDTO) {
		String methodName = "用户注册接口";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, registerReqDTO);
		Result<RegisterResDTO> result;
		try {
			result = userService.register(registerReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 用户登陆接口，登陆成功返回token
	 */
	@PostMapping(value = "/login")
	@ApiOperation(value = "登录",httpMethod = "POST",notes = "登录后返回token")
	public Result<LoginResDTO> login(@RequestBody LoginReqDTO loginReqDTO,HttpServletRequest request) {
		String methodName = "用户登陆接口";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, loginReqDTO);
		Result<LoginResDTO> result;
		try {
			result = userService.login(loginReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 查询当前登录用户信息
	 */
	@GetMapping(value = "/queryUserInfo")
	public Result<QueryUserInfoResDTO> queryUserInfo(Principal principal) {
		String methodName = "获取当前登录用户信息接口";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, principal);
		Result<QueryUserInfoResDTO> result;
		try {
			result = userService.queryUserInfo(principal);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 登出接口
	 */
	@PostMapping(value = "/logout")
	public Result<Boolean> logout() {
		return new Result<>(Boolean.TRUE);
	}

	/**
	 * 查询指定用户信息
	 */
	@PostMapping(value = "/queryUserInfoById")
	public Result<QueryUserInfoByIdResDTO> queryUserInfoById(@RequestBody QueryUserInfoByIdReqDTO queryUserInfoByIdReqDTO) {
		String methodName = "查询指定用户信息接口";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, queryUserInfoByIdReqDTO);
		Result<QueryUserInfoByIdResDTO> result;
		try {
			result = userService.queryUserInfoById(queryUserInfoByIdReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 修改用户信息
	 */
	@PostMapping(value = "/modifyUserInfo")
	public Result<ModifyUserInfoResDTO> modifyUserInfo(@RequestBody ModifyUserInfoReqDTO modifyUserInfoReqDTO) {
		String methodName = "修改用户信息接口";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, modifyUserInfoReqDTO);
		Result<ModifyUserInfoResDTO> result;
		try {
			result = userService.modifyUserInfo(modifyUserInfoReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 修改用户密码
	 */
	@PostMapping(value = "/updatePassword")
	public Result<ModifyPasswordResDTO> modifyPassword(@RequestBody ModifyPasswordReqDTO modifyPasswordReqDTO) {
		String methodName = "修改用户密码";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, modifyPasswordReqDTO);
		Result<ModifyPasswordResDTO> result;
		try {
			result = userService.modifyPassword(modifyPasswordReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 删除用户信息
	 */
	@PostMapping(value = "/deleteUser")
	public Result<DeleteUserResDTO> deleteUser(@RequestBody DeleteUserReqDTO deleteUserReqDTO) {
		String methodName = "删除用户接口";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, deleteUserReqDTO);
		Result<DeleteUserResDTO> result;
		try {
			result = userService.deleteUser(deleteUserReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 给用户分配角色
	 */
	@PostMapping(value = "/allotRole")
	public Result<AllotRoleResDTO> allotRole(@RequestBody AllotRoleReqDTO allotRoleReqDTO) {
		String methodName = "给用户分配角色接口";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, allotRoleReqDTO);
		Result<AllotRoleResDTO> result;
		try {
			result = userService.allotRole(allotRoleReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}

	/**
	 * 查询用户的角色
	 */
	@PostMapping(value = "/queryUserRoles")
	public Result<QueryUserRolesResDTO> queryUserRoles(@RequestBody QueryUserRolesReqDTO queryUserRolesReqDTO) {
		String methodName = "查询用户的角色";
		long startTime = System.currentTimeMillis();
		LogUtil.printStartLogInfo(log, methodName, queryUserRolesReqDTO);
		Result<QueryUserRolesResDTO> result;
		try {
			result = userService.queryUserRoles(queryUserRolesReqDTO);
		} catch (Exception e) {
			result = ExceptionUtil.dealException(e);
		}
		LogUtil.printEndLogInfo(log, methodName, startTime, result);
		return result;
	}
	@PostMapping(value = "/findUserByToken")
	public String findUserByToken(HttpServletRequest request){
		String userNameFromToken = tokenManager.getUserNameFromToken(request.getHeader(JwtConstants.TOKEN_HEADER).substring(6));
		return userNameFromToken;
	}
	@PostMapping(value = "/noctice/getNotice")
	public ResultEntity QueryreadByToken(HttpServletRequest request, @RequestParam("pageSize")Integer pageSize, @RequestParam("pageNum")Integer pageNum, @RequestParam(value = "type",defaultValue = "3") Integer type){
		String userNameFromToken = tokenManager.getUserNameFromToken(request.getHeader(JwtConstants.TOKEN_HEADER).substring(6));
		QueryWrapper<TUser> wapper = new QueryWrapper<>();
		wapper.eq("username",userNameFromToken);
		TUser one = userService.getOne(wapper);
		Page<TUser> pageSizePage = new Page<>(pageNum , pageSize);
		return null;
	}
}
